package com.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.biz.NewsBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.News;
import com.entity.vo.VideoVO;
import com.entity.vo.VideoVO2;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by jc on 2019/11/16.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController("apiHKVideoController")
@RequestMapping("/api/HkVideo")
public class HKVideoController {
    @PostConstruct
    public void auto() {
        ArtemisConfig.host = "124.160.90.70:21443"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "27611787"; // 秘钥appkey
        ArtemisConfig.appSecret = "NjTw6CNu6LQ3w3vE5kU3";// 秘钥appSecret
    }

    private static final String CONTENT_TYPE = "application/json";

    private static final String ARTEMIS_PATH = "/artemis";
    //查询监控点列表
    private static final String CAMERA_LIST_API = ARTEMIS_PATH + "/api/resource/v1/cameras";
    //获取监控点预览取流URL
    private static final String PREVIEW_URLS_API = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";

    private static final String cameraStateApi = ARTEMIS_PATH + "/api/nms/v1/online/camera/get";//获取监控点在线状态


    @RequestMapping(value = "/getCameraList")
    public Object getCameraList(String longitude, String latitude) {
        Map path = new HashMap(2) {
            {
                put("https://", CAMERA_LIST_API);
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 1000);
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE, null);
        JSONObject js = JSON.parseObject(result);
        JSONObject data1 = js.getJSONObject("data");
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) data1.get("list");
        Map<String, Integer> cameraState = getCameraStateList();

        return data1;
    }


    @RequestMapping(value = "/getUrl")
    public Result getUrl() {
        /*Map<String, String> map = new HashMap<>();
        map.put("驾云台鹰眼2", getPreviewURLs("baebaba7498e4e1f8e599d96279c2839"));
        map.put("驾云台鹰眼1", getPreviewURLs("d10d036cbb9b495b9c9793800aff9ff2"));
        map.put("钢铁长城鹰眼2", getPreviewURLs("1f21a7115df04343aa9aa56238609e40"));
        map.put("钢铁长城鹰眼1", getPreviewURLs("3c76e19b32f9436d8fa4d06be8cf1219"));*/

        List<VideoVO2> videoVO2s = new ArrayList<>();

        VideoVO2 videoVO2 = new VideoVO2();
        videoVO2.setName("驾云台鹰眼2");
        videoVO2.setUrl(getPreviewURLs("baebaba7498e4e1f8e599d96279c2839"));
        videoVO2s.add(videoVO2);

        videoVO2 = new VideoVO2();
        videoVO2.setName("驾云台鹰眼1");
        videoVO2.setUrl(getPreviewURLs("d10d036cbb9b495b9c9793800aff9ff2"));
        videoVO2s.add(videoVO2);

        videoVO2 = new VideoVO2();
        videoVO2.setName("钢铁长城鹰眼2");
        videoVO2.setUrl(getPreviewURLs("1f21a7115df04343aa9aa56238609e40"));
        videoVO2s.add(videoVO2);

        videoVO2 = new VideoVO2();
        videoVO2.setName("钢铁长城鹰眼1");
        videoVO2.setUrl(getPreviewURLs("3c76e19b32f9436d8fa4d06be8cf1219"));
        videoVO2s.add(videoVO2);


        return ResultGenerator.genSuccessResult(videoVO2s);
    }


    public String getPreviewURLs(String indexCode) {
        Map<String, String> map = new HashMap<>();

        Map path = new HashMap(2) {
            {
                put("https://", PREVIEW_URLS_API);
            }
        };
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("streamType", 0);
        jsonObject.put("protocol", "hls");
        jsonObject.put("transmode", 1);
        jsonObject.put("expand", "transcode=1&videotype=h264");
        jsonObject.put("cameraIndexCode", indexCode);

        String body = jsonObject.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE, null);

        if (null != result) {
            jsonObject = JSON.parseObject(result);
            if (jsonObject.get("code").equals("0")) {
                String url = JSON.parseObject(JSON.toJSONString(jsonObject.get("data"))).get("url").toString();
                return url;
            }
        }
        return null;
    }

    /**
     * 获取所有监控点的在线、离线状态
     *
     * @return
     */
    public Map<String, Integer> getCameraStateList() {

        Map path = new HashMap(2) {
            {
                put("https://", cameraStateApi);
            }
        };
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", 1000);
        String body = jsonObject.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, CONTENT_TYPE, null);
        JSONObject js = JSON.parseObject(result);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) JSON.parseObject(JSON.toJSONString(js.get("data"))).get("list");
        Map stateMap = new HashMap();
        dataList.stream().forEach(obj -> stateMap.put(obj.get("indexCode"), obj.get("online")));
        return stateMap;
    }
}
